import 'dart:convert';
import 'package:flutter/cupertino.dart';
/**
 * @Author: zhouge
 * @Description:  首页导航栏的显示
 * @Date: Created in 0:05 2020-05-05
 * @Modified By:
 **/

import 'package:flutter/material.dart';
import '../config.dart';
import '../home/conversion.dart';
import '../home/contacts_page.dart';
import 'package:dio/dio.dart';
import 'discover_page.dart';
import '../home/functions_page.dart';

// 枚举类型
enum ActionItems { GROUP_CHAT, ADD_PERSON, QR_SCAN, PAYMENT, HELP }

class ScaffoldRoute extends StatefulWidget {
  @override
  _ScaffoldRouteState createState() => _ScaffoldRouteState();
}

class _ScaffoldRouteState extends State<ScaffoldRoute> {
  PageController _pageController;
  List<Widget> _pages;
  int _currentIndex = 0;

  @override
  void initState() {
    super.initState();
    _pageController = PageController(initialPage: _currentIndex);
    _pages = [
      ConversationPage(),   //微信对话层
      ContactsPage(),
      DiscoverPage(),
      FunctionsPage(),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //顶部菜单栏
      appBar: AppBar(
        title: Text('微信'),
        elevation: 0.0,
        actions: <Widget>[
          //导航栏的右侧菜单
          IconButton(
            icon: Icon(MyIcons.zoom),
            onPressed: () {
              print('感谢分享！');
            },
          ),

          // 主要用来做隔断处理
          Container(width: 16.0),

          // 弹出菜单栏位
          PopupMenuButton(
            color: Color(AppColors.AppBarColor),
            itemBuilder: (BuildContext context) {
              return <PopupMenuItem<ActionItems>>[
                PopupMenuItem(
                    child: _buildPopuMenuItem(Icon(MyIcons.all_chat), '发起群聊'),
                    value: ActionItems.GROUP_CHAT),
                PopupMenuItem(
                    child: _buildPopuMenuItem(Icon(MyIcons.add_person), '添加朋友'),
                    value: ActionItems.ADD_PERSON),
                PopupMenuItem(
                    child: _buildPopuMenuItem(Icon(MyIcons.scan), '扫一扫'),
                    value: ActionItems.QR_SCAN),
                PopupMenuItem(
                    child: _buildPopuMenuItem(Icon(MyIcons.all_chat), '收付款'),
                    value: ActionItems.PAYMENT),
                PopupMenuItem(
                    child: _buildPopuMenuItem(Icon(MyIcons.help), '帮助与反馈'),
                    value: ActionItems.HELP)
              ];
            },
            icon: Icon(MyIcons.add),
            onSelected: (ActionItems selected) {
              print('点击的是$selected');
            }, //选择事件
          ),
        ],
      ),
      body: PageView.builder(
        itemBuilder:(BuildContext context,int index){
            return _pages[index];
          },
        controller: _pageController,
        itemCount: _pages.length,
        onPageChanged: (int index){
          setState(() {
            _currentIndex = index;
          });
        },
      ),

      // 底部菜单栏
      bottomNavigationBar: BottomNavigationBar(
        items: [
          BottomNavigationBarItem(
              icon: Icon(MyIcons.wechat),
              title: Text("微信", style: TextStyle(fontSize: 14.0)),
              activeIcon: Icon(MyIcons.drak_wechat)),
          BottomNavigationBarItem(
              icon: Icon(MyIcons.address),
              title: Text("通讯录", style: TextStyle(fontSize: 14.0),),
              activeIcon: Icon(MyIcons.address)),
          BottomNavigationBarItem(
              icon: Icon(MyIcons.discover),
              title: Text("发现", style: TextStyle(fontSize: 14.0),),
              activeIcon: Icon(MyIcons.discover,)),
          BottomNavigationBarItem(
              icon: Icon(MyIcons.owner),
              title: Text("我", style: TextStyle(fontSize: 14.0),),
              activeIcon: Icon(MyIcons.drak_owner,)),
        ],
        type: BottomNavigationBarType.fixed, //底部导航栏超过3个就不会显示颜色了，需要加type
        currentIndex: _currentIndex,
        fixedColor: Color(AppColors.TabIconActiveColor), // 字体和图表一起变颜色
        selectedFontSize: 12.0,
        onTap: _onItemTapped, //点击的时候进行切换
      ),
    );
  }

  // 创建顶部+号的按钮菜单
  _buildPopuMenuItem(Widget icon, String title) {
    return Row(
      children: <Widget>[
        icon,
        Container(
          width: 12.0,
        ),
        Text(
          title,
          style: TextStyle(color: Colors.white),
        )
      ],
    );
  }


  //按钮事件切换
  void _onItemTapped(int index) {
    setState(() {

/*      // http测试
      void getHttp() async {
        try {
          Response response = await Dio().get("http://118.31.77.155/api/mes");
          json.decode(response.toString());
          print(response);
        } catch (e) {
          print(e);
        }
      };
 */
      _currentIndex = index;
      print("第$index个图标");
  //    getHttp();
      _pageController.animateToPage(
          _currentIndex,
          duration: Duration(milliseconds: 200),
          curve: Curves.easeInOut);
    });
  }

}
